<template>
  <div class="modal-form-container">
    <Form ref="info" :model="menuData" :rules="rules" :label-width="100">
      <FormItem :label="$t('HfDg9OexYXAbDV3inAO6R')" prop="name">
        <Select v-if="status === 'new'" v-model="menuData.name" @on-change="changeMenu">
          <Option
            v-for="menu in menuLists"
            :key="menu.name"
            :value="menu.name"
            >{{ menu.title }}({{ menu.name }})</Option
          >
        </Select>
        <Input v-else disabled v-model="menuData.name" />
      </FormItem>
      <FormItem :label="$t('1m5Mrf5otrghObt6WEnHU')">
        <Row>
          <Col span="22">
            <Input v-model="menuData.urlname" />
          </Col>
          <Col offset="1" span="1">
            <el-tooltip
              effect="dark"
              :content="$t('pK1l721yoK_z6vgXt5qPF')"
            >
              <i class="el-icon-info"></i>
            </el-tooltip>
          </Col>
        </Row>
      </FormItem>
      <FormItem :label="$t('vzJyP1frPrz_UYCIF603L')" prop="titlename">
        <Input v-model="menuData.titlename" />
      </FormItem>
      <FormItem v-if="parent && parent.name" :label="$t('lwjOeFVMnIEr-OdlXnwxY')" prop="titlename">
        <Input disabled v-model="parent.titlename" />
      </FormItem>
      <FormItem :label="$t('BanFOXoRcUwxeJvr8DTNW')" prop="sort">
        <Input v-model="menuData.sort" />
      </FormItem>
      <FormItem>
        <Button type="primary" @click="save">{{$t('q-FETLOZVSMbWnrNq0vj6')}}</Button>
        <Button style="margin-left: 8px" @click="close">{{$t('2LFpg56tAgKyBB40xpv5l')}}</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { addMenu, updateMenu } from '@/api/data2'
import { rootMenu as menuTree, menuList } from '@/router/routers'

const findMenu = (list, menu) => {
  const l = list.find(l => l.name === menu.pname)
  if (l) {
    return l
  } else {
    if (list.children && list.children.length) {
      return findMenu(list.children, menu)
    }
    return null
  }
}

const isFalse = p => {
  return _.isNil(p) || p === '' || p == 0
}

export default {
  name: 'routemenu-form',
  props: {
    data: Object,
    rules: Object,
    status: String,
    show: Boolean,
    parent: Object
  },
  data () {
    return {
      menuData: _.cloneDeep(this.data)
      // urlname: ''
    }
  },
  created () {
    this.getMenuLists()
  },
  watch: {
    // 'menuData.name' (val) {
    //   const menu = menuList.find(m => m.name === val)
    //   const root = menuTree.find(m => m.name === val)
    //   // this.urlname = menu ? menu.path : ''
    //   this.menuData.urlname = root
    //     ? root.path || root.children[0].path
    //     : menu
    //       ? menu.path
    //       : ''
    // },
    show (val) {
      if (val) {
        this.menuData = _.cloneDeep(this.data)
        this.getMenuLists()
      }
    }
  },
  methods: {
    changeMenu () {
      const menu = this.menuLists.find(m => m.name == this.menuData.name)
      this.menuData = {
        name: menu.name,
        urlname: menu.path || menu.children[0].path,
        titlename: menu.title,
        id: this.menuData.id,
        pid: this.menuData.pid
      }
    },
    getMenuLists () {
      if (this.status === 'new') {
        if (isFalse(this.menuData.pid)) {
          this.menuLists = _.cloneDeep(menuTree.filter(m => ['frontMain', 'pm'].indexOf(m.name) == -1))
        } else {
          // const menu = findMenu(menuTree, this.menuData)
          // this.menuLists = menu?.children || []
          this.menuLists = _.cloneDeep(
            menuList.filter(m => {
              return ['frontMain', 'pm'].indexOf(m.pname) == -1
            })
          )
        }
      }
    },
    save () {
      this.$refs.info.validate(async valid => {
        if (valid) {
          const { name, titlename, urlname, pid, id, sort } = this.menuData
          const data = { name, titlename, urlname, pid, sort: +sort }
          const { code, desc } =
            this.status === 'new' ? await addMenu(data) : await updateMenu(_.assign({}, data, { id }))
          if (code == 200) {
            this.$emit('success', data)
          } else {
            this.$emit('fail', desc)
          }
        } else {
          this.$Message.error(this.$t('fcmS3YBVNyvO3V68Qt3xl'))
        }
      })
    },
    close () {
      this.$emit('close')
    }
  }
}
</script>

<style lang="stylus" scoped>
.modal-form-container
  height auto
</style>
